// Mongoose是在node.js环境下对mongodb进行便捷操作的对象模型工具

// 通过 mongoose 来链接mongodb数据库  (插件)

// mongoose使mongodb操作更简单便捷

// connect 用于创建数据库连接

// db.createUser({user:'zuozuomu',pwd:'missyou',roles: [{role:'readWrite',db:'wh2101'}]})

var mongoose = require("mongoose")

var hostname = "101.37.167.214"
var port = 27017
// var user = "llc"
// var pass = "sdyb"
var dbname = "testbase"

// const CONN_DB_STR = `mongodb://${user}:${pass}@${hostname}:${port}/${dbname}`
const CONN_DB_STR = `mongodb://${hostname}:${port}/${dbname}`

//mongoose.connect('mongodb://用户名:密码@127.0.0.1:27017/数据库')


mongoose.connect(CONN_DB_STR, {
    useUnifiedTopology: true,
    useNewUrlParser: true
}, (err) => {
    if (err) {
        console.log("数据库连接失败")
        throw err
    } else {
        console.log("mongodb 数据库连接成功");
    }
})



// on 监听 mongodb 数据库的操作
/*
    连接成功

*/
mongoose.connection.on('connected', function () {
    console.log('mongoose connection open to' + CONN_DB_STR);
});

/*
 连接异常
*/

mongoose.connection.on('error', function (err) {
    console.log('mongoose connection error:' + err);
});

/**
 * 连接断开
 *
 */
mongoose.connection.on('disconnected', function () {
    console.log('mongoose connection disconnected');
})